<?php 
class log extends Model{
	private $table="xdf_log";
	private $fields=array(
			"id",
			"user_id",
			"action_id",
			"resource_type",
			"resource_id",
			"content",
			"state",
			"msg",
			"create_time"
	);

	//controllers
	public $controllers=array(
			"Account"	=>1,
			"Common"	=>2,
			"Course"	=>3,
			"Feedback"	=>4,
			"Homework"	=>5,
			"Item"		=>6,
			"Message"	=>7,
			"Person"	=>8,
			"Prepare"	=>9,
			"Push"		=>10,
			"Video"		=>11
	);

	//actions
	public $actions=array(
		"Play"=>1
	);
	public $resource_type=array(
		"video" =>1,
		"note"  =>2
	);
	//根据controller和action获取action的id
	private function getAIdByCA($c_str,$a_str){
		return 5;	//观看视频的action_id 暂时设为5
		$sql="SELECT id FROM xdf_action 
			WHERE name=$a_str AND ctrl_id IN (
				SELECT id FROM xdf_controller WHERE name=$c_str);";
		return $this->fetchRow($sql);
	}
	//添加一条用户日志                      $user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$ms
	public function addLog($user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg){
		//获取action的id
		$a_id=$this->getAIdByCA($c_str,$a_str);
		$sql="INSERT INTO $this->table (user_id,action_id,resource_type,resource_id,content,state,msg,create_time)
			VALUES('$user_id','$a_id','$resource_type','$resource_id','$content','$state','$msg',NOW());";
// 		print_r($sql);
		return $this->insert($sql);
	}
	//添加一条用户日志                      $user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$ms
	public function test($user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg){
		//获取action的id
		$a_id=$this->getAIdByCA($c_str,$a_str);
		$sql="INSERT INTO $this->table (user_id,action_id,resource_type,resource_id,content,state,msg,create_time) VALUES ('$user_id','$a_id','$resource_type','$resource_id','$content','$state','$msg',NOW() );";
// 		print_r($sql);
		return $sql;
	}
	public function getLog($user_id,$c_str,$a_str,$resource_type,$resource_id){
		$a_id=$this->getAIdByCA($c_str,$a_str);
		$sql="SELECT *FROM  $this->table 
		WHERE user_id=$user_id AND action_id=$a_id AND resource_type=$resource_type AND resource_id=$resource_id;";

		return $this->fetchAll($sql);
	}
	/*demo
	 * 获取一个学生是否看过某个视频
	 * $log=new log();
	 * $isViewed=$log->getLog($user_id,"Video","Play",1,$video_id);
	 * 
	 */
	
	//统计我看过的所有课程视频总数
	public function getMyTotalVideo($user_id){
		$sql="SELECT id,user_id,action_id,resource_type,resource_id,create_time,count(resource_id)
				FROM xdf_log 
				WHERE user_id=$user_id AND action_id=5 
				AND resource_type=1 AND resource_id in (
					SELECT video_id FROM `xdf_period` 
					WHERE `video_id` IS NOT NULL AND `class_id` IN (
						SELECT `class_id` FROM `xdf_class_student` WHERE `student_id`= '$user_id' 
					)
				)  
				group by resource_id ;";
		$result=$this->fetchAll($sql);
		if($result)
			return count($result);
		else 
			return 0;
	}
	//统计我的所有课程视频总数
	public function getTotalVideo($user_id){
		$sql="SELECT video_id FROM `xdf_period` 
			WHERE `video_id` IS NOT NULL AND `class_id` IN (
				SELECT `class_id` FROM `xdf_class_student` WHERE `student_id`= '$user_id' 
			);";
		$result=$this->fetchAll($sql);
		if($result)
			return count($result);
		else 
			return 0;
	}
}

?>